package com.changxianggu.crawler.cloud.test;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.changxianggu.cloud.crawler.core.WebApplication;
import com.changxianggu.cloud.crawler.core.modules.storage.entity.CxgBook;
import com.changxianggu.cloud.crawler.core.modules.storage.service.ICxgBookService;
import com.changxianggu.cloud.crawler.core.utils.HttpUtil;
import com.changxianggu.cloud.crawler.core.utils.UserAgentUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {WebApplication.class})// 指定启动类
@Slf4j
public class GuoFangGongYe {

    @Autowired
    ICxgBookService bookService;

    public static final String COOKIE = "ASP.NET_SessionId=5r3o3hqny2enflxhlimqtgvm";
    public static final String ORIGIN = "http://e.guofangyun.net";
    public static final String REFERER = "http://e.guofangyun.net/List/Default.aspx";
    public static final String URL_PRE = "http://e.guofangyun.net/";

    @Test
    public void initCrawler() throws Exception {
        doCrawler();
    }

    public void doCrawler() throws Exception {
        int pageNum =1;
        int totalPage = 286;
        int pageSize = 24;
        for (int i = pageNum; i <= totalPage; i++) {
            String url = "http://e.guofangyun.net/WebServices/ListWebService.asmx/GetBookList";
            Map<String, Object> parameter = MapUtil.newHashMap(4);
            parameter.put("startPage", i);
            parameter.put("Size", pageSize);
            parameter.put("bookState", "255");
            parameter.put("pageSize", pageSize);
            Map<String, Object> headers = MapUtil.newHashMap(5);
            headers.put("Cookie", COOKIE);
            headers.put("ORIGIN", ORIGIN);
            headers.put("User-Agent", UserAgentUtil.random());
            headers.put("Referer", REFERER);
            String result = HttpUtil.doPost(url, headers, parameter);
            if (StrUtil.isNotBlank(result)) {
                JSONObject jsonObject = JSONObject.parseObject(result);
                JSONArray dataList = jsonObject.getJSONArray("DataList");
                if (dataList != null && dataList.size()>0) {
                    int size = dataList.size();
                    List<CxgBook> listBook = new ArrayList<>();
                    for (int k = 0; k < size; k++) {
                        JSONObject jsonTmp = dataList.getJSONObject(k);
                        String pkId = jsonTmp.getString("ID");
                        CxgBook bookEntity = new CxgBook();

                        bookEntity.setIsSync(0);
                        bookEntity.setCreateTime(LocalDateTime.now());
                        bookEntity.setPressName("国防工业出版社");
                        bookEntity.setStatus(1);
                        bookEntity.setPressId(36);
                        CxgBook tempEntity = getBookEntity(pkId,bookEntity);
                        listBook.add(tempEntity);
                    }
                    bookService.saveBatch(listBook);
                    log.info("第" + i + "页数据保存成功");
                    log.warn("--->" + i);
                }
            }
        }
    }

    public CxgBook getBookEntity(String numId,CxgBook bookEntity) throws Exception {
        String url = "http://e.guofangyun.net/WebServices/DetailPageWebService.asmx/GetDetail";

        Map<String, Object> parameter = MapUtil.newHashMap(2);
        parameter.put("id", numId);
        Map<String, Object> headers = MapUtil.newHashMap(5);
        headers.put("Cookie", COOKIE);
        headers.put("User-Agent", UserAgentUtil.random());
        headers.put("Referer", REFERER);
        String result = HttpUtil.doPost(url, headers,parameter);
        JSONObject jsonObject = JSONObject.parseObject(result);
        if (jsonObject != null) {;
            if (jsonObject != null) {
                bookEntity.setPkId(numId);
                bookEntity.setBookName(jsonObject.getString("书名"));
                bookEntity.setBookIsbn(jsonObject.getString("ISBN").replace("-",""));
                bookEntity.setPrice(new BigDecimal(jsonObject.getString("定价")));
                bookEntity.setCover(jsonObject.getString("ImagePath"));
                bookEntity.setAuthor(jsonObject.getString("作者"));
                bookEntity.setPrintVersion(jsonObject.getString("印次"));
                bookEntity.setBookVersion(jsonObject.getString("版次"));
                if (StrUtil.isNotBlank(getNumbers(jsonObject.getString("页数")))) {
                    bookEntity.setPageNum(Integer.parseInt(getNumbers(jsonObject.getString("页数"))));
                }
                if (StrUtil.isNotBlank(getNumbers(jsonObject.getString("千字数")))) {
                    bookEntity.setWordNum(Integer.parseInt(getNumbers(jsonObject.getString("千字数"))));
                }
                bookEntity.setBrief(jsonObject.getString("简介"));
                JSONArray cataObj = jsonObject.getJSONArray("Catalogs");
                String Catalogs = "";
                for (int m = 1;m<cataObj.size();m++) {
                    Catalogs+=cataObj.getJSONObject(m).getString("Title")+"   ";
                }
                try {
                    String pubDateStr = jsonObject.getString("出版时间").replace("年","-").replace("月","-").replace("日","");
                    String[] dateArr = pubDateStr.split("-");
                    String month = dateArr[1];
                    String day = dateArr[2];
                    if (Integer.parseInt(dateArr[1])<10) {
                        month = "0"+ month;
                    }
                    if (Integer.parseInt(dateArr[2])<10) {
                        day = "0"+ day;
                    }
                    String pubDate = dateArr[0]+ "-" + month + "-" +day+ " 00:00:00";
                    DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                    LocalDateTime ldt = LocalDateTime.parse(pubDate,df);
                    bookEntity.setPubDate(ldt);
                }catch (Exception e) {

                }
                bookEntity.setCatalog(Catalogs);
                bookEntity.setCover(jsonObject.getString("Imgurl"));
                bookEntity.setDigitalUri(URL_PRE+jsonObject.getString("ReadlineUrl"));
            }
        }
        return bookEntity;
    }

    /**
     * 判断一个字符串是否都为数字
     *
     * @param strNum
     * @return
     */
    public boolean isDigit(String strNum) {
        Pattern pattern = Pattern.compile("[0-9]{1,}");
        Matcher matcher = pattern.matcher((CharSequence) strNum);
        return matcher.matches();
    }

    /**
     * 截取数字
     *
     * @param content
     * @return
     */
    public String getNumbers(String content) {
        Pattern pattern = Pattern.compile("\\d+");
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group(0);
        }
        return "";
    }
}
